#define __LIBRARY__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>   
#include <errno.h>
/*此处本应添加#include <pthread.h>,但是memtest.c中已经添加，因此为避免重复定义，暂不添加*/             	         /*包含自制的库文件，其中有系统调用API*/

#define PAGE_SIZE 4096

int pthread_create(pthread_t *thread,const pthread_attr_t* attr,void* (*start_routine)(void *),void* arg)
{
	pthread_t thread_id;
	
	if(thread == NULL || start_routine == NULL)
	{
		printf("Error:Thread or func_addr is NULL!!!\n");
		exit(0);
	}

	if(attr != NULL && attr->stacksize > PAGE_SIZE)
		return EINVAL;
	
	thread_id = thread_fork(attr,start_routine,arg);
	if(thread_id == -1)
	{
		printf("Error:New thread creating failed!!!\n");
		return errno;
	}

	if(thread_id > 0)
	{
		(*thread) = thread_id;
		return 0;
	}

	printf("Error:pthread_create error!\n");
	return -1;
}

int pthread_join(pthread_t thread,void** value_ptr)
{
	if(thread <=0)
	{
		printf("Error:Invalid thread for pthread_join!\n");
		exit(0);
	}

	if(value_ptr == NULL)
	{
		printf("Error:value_ptr is invalid!\n");
		exit(0);
	}	
		
	thread_join(thread,value_ptr);

	return 0;
}

void pthread_exit(void* value_ptr)
{
	thread_exit(value_ptr);
}






















